package com.dch.calculator.activities.exception;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.dch.calculator.R;
import com.dch.calculator.activities.MainActivity;
import com.dch.calculator.activities.app.CalculatorActivity;

public class ExceptionActivity extends Activity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exception_main);

        Intent intent = getIntent();

        TextView titleView = (TextView) findViewById(R.id.exception_title);
        String title = intent.getStringExtra("title");
        if (title == null || title.equals("")) {
            title = getResources().getString(R.string.error_title);
        }
        titleView.setText(title);

        TextView detailsView = (TextView) findViewById(R.id.exception_detail);
        String details = intent.getStringExtra("detail");
        if (details == null || details.equals("")) {
            details = getResources().getString(R.string.error_detail);
        }
        String TEMPLATE = getResources().getString(R.string.error_from);
        String from = intent.getStringExtra("from");
        if (from == null || from.equals("")) {
            from = CalculatorActivity.class.getName();
        }
        details = details.concat("\n").concat(String.format(TEMPLATE, from));
        detailsView.setText(details);

        TextView yes = (TextView) findViewById(R.id.exception_yes);
        TextView cancel = (TextView) findViewById(R.id.exception_no);
        yes.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.exception_yes: {
                finish();
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                break;
            }
            default: {
                finish();
            }
        }
    }
}
